package com.longdaji.decoration.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.utils.LogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Kanade on 2018/4/19 0019.
 */

public class AllOrderAdapter extends RecyclerView.Adapter<AllOrderAdapter.OrderHolder> {
    private List<Orders.OrdersBean.OrderBean> mOrderList;
    private Context mContext;


    public AllOrderAdapter(List<Orders.OrdersBean.OrderBean> orderList) {
        this.mOrderList = orderList;
    }

    class OrderHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_total_count)
        TextView tvTotalCount;
        @BindView(R.id.tv_total_price)
        TextView tvTotalPrice;
        @BindView(R.id.tv_total_price_at_last)
        TextView tvTotalPriceAtLast;
        @BindView(R.id.rv_product)
        RecyclerView rvProduct;
        @BindView(R.id.tv_order_status)
        TextView tvOrderStatus;
        @BindView(R.id.rl_payment)
        RelativeLayout rlPayment;
        @BindView(R.id.rl_delivery)
        RelativeLayout rlDelivery;
        @BindView(R.id.rl_receiving)
        RelativeLayout rlReceiving;
        @BindView(R.id.rl_evaluate)
        RelativeLayout rlEvaluate;

        public OrderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick({R.id.btn_pay, R.id.btn_cancel_order_payment, R.id.btn_cancel_order_delivery,
                R.id.btn_see_logistics_receiving, R.id.btn_confirm_receiving,
                R.id.btn_return_goods, R.id.btn_see_logistics_evaluate, R.id.btn_evaluate})
        void onItemViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_pay:
                    mListener.onPayClick(getAdapterPosition());
                    break;
                case R.id.btn_cancel_order_payment:
                    mListener.onCancelOrderPaymentClick(getAdapterPosition());
                    break;
                case R.id.btn_cancel_order_delivery:
                    mListener.onCancelOrderDeliveryClick(getAdapterPosition());
                    break;
                case R.id.btn_see_logistics_receiving:
                    mListener.onSeeLogisticsReceivingClick(getAdapterPosition());
//                    mSeeLogisticsReceivingListener.onSeeLogisticsReceivingClick(getAdapterPosition());
                    break;
                case R.id.btn_confirm_receiving:
                    mListener.onConfirmReceivingClick(getAdapterPosition());
                    break;
                case R.id.btn_return_goods:
                    mListener.onRefundGoodsClick(getAdapterPosition());
                    break;
                case R.id.btn_see_logistics_evaluate:
                    mListener.onSeeLogisticsEvaluateClick(getAdapterPosition());
//                    mSeeLogisticsEvaluateListener.onSeeLogisticsEvaluateClick(getAdapterPosition());
                    break;
                case R.id.btn_evaluate:
                    mListener.onEvaluateClick(getAdapterPosition());
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public OrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_all_order, parent, false);
        OrderHolder holder = new OrderHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(OrderHolder holder, int position) {
        Orders.OrdersBean.OrderBean order = mOrderList.get(position);
        ProductAdapter productAdapter = new ProductAdapter(order.getProducts());
        int totalCount = order.getProductCount();
        double totalPrice = order.getProductAmountTotal();
        String totalPriceAtLast = order.getOrderAmountTotal();
        int orderStatus = order.getOrderStatus();
        String orderCarriage = order.getOrderCarriage();
        // TODO: 2018/4/19 0019 运费
        if (orderCarriage == null) {
            orderCarriage = "0.0";
        }
        LogUtil.d("totalCount : " + totalCount + "  totalPrice : " + totalPrice + "orderCarriage : " + orderCarriage);
        holder.tvTotalCount.setText("共" + String.valueOf(totalCount) + "件商品" + "  ");
        holder.tvTotalPrice.setText("合计￥" + String.valueOf(totalPrice) + "(含运费￥");
        holder.tvTotalPriceAtLast.setText(orderCarriage + ")");
        holder.rvProduct.setAdapter(productAdapter);
        holder.rvProduct.setLayoutManager(new LinearLayoutManager(mContext));
        switch (orderStatus) {
            case Constants.ORDER_PENDING_PAYMENT:
                holder.tvOrderStatus.setText("等待付款中");
                holder.rlPayment.setVisibility(View.GONE);
                holder.rlDelivery.setVisibility(View.GONE);
                holder.rlReceiving.setVisibility(View.GONE);
                holder.rlEvaluate.setVisibility(View.GONE);
                holder.rlPayment.setVisibility(View.VISIBLE);
                break;
            case Constants.ORDER_PENDING_DELIVERY:
                holder.rlPayment.setVisibility(View.GONE);
                holder.rlDelivery.setVisibility(View.GONE);
                holder.rlReceiving.setVisibility(View.GONE);
                holder.rlEvaluate.setVisibility(View.GONE);
                holder.tvOrderStatus.setText("等待卖家发货");
                holder.rlDelivery.setVisibility(View.VISIBLE);
                break;
            case Constants.ORDER_PENDING_RECEIVING:
                holder.rlPayment.setVisibility(View.GONE);
                holder.rlDelivery.setVisibility(View.GONE);
                holder.rlReceiving.setVisibility(View.GONE);
                holder.rlEvaluate.setVisibility(View.GONE);
                holder.tvOrderStatus.setText("卖家已发货");
                holder.rlReceiving.setVisibility(View.VISIBLE);
                break;
            case Constants.ORDER_FINISHED:
                holder.rlPayment.setVisibility(View.GONE);
                holder.rlDelivery.setVisibility(View.GONE);
                holder.rlReceiving.setVisibility(View.GONE);
                holder.rlEvaluate.setVisibility(View.GONE);
                holder.tvOrderStatus.setText("交易已完成");
                holder.rlEvaluate.setVisibility(View.VISIBLE);
                break;
            case Constants.ORDER_CANCELD:
                holder.rlPayment.setVisibility(View.GONE);
                holder.rlDelivery.setVisibility(View.GONE);
                holder.rlReceiving.setVisibility(View.GONE);
                holder.rlEvaluate.setVisibility(View.GONE);
                holder.tvOrderStatus.setText("订单已取消");
                break;
            default:
                break;
        }

    }

    @Override
    public int getItemCount() {
        return mOrderList.size();
    }

    public void setData(List<Orders.OrdersBean.OrderBean> list) {
        mOrderList.clear();
        mOrderList.addAll(list);
        notifyDataSetChanged();
    }

    public interface AdapterItemClickListener {
        void onPayClick(int position);

        void onCancelOrderPaymentClick(int position);

        void onCancelOrderDeliveryClick(int position);

        void onSeeLogisticsReceivingClick(int position);

        void onConfirmReceivingClick(int position);

        void onRefundGoodsClick(int position);

        void onSeeLogisticsEvaluateClick(int position);

        void onEvaluateClick(int position);

    }

    private AdapterItemClickListener mListener;

    public void setOnAdapterItemClickListener(AdapterItemClickListener listener) {
        this.mListener = listener;
    }

    public interface PayListener {
        void onPayClick(int position);
    }

    public interface CancelOrderPaymentListener {
        void onCancelOrderPaymentClick(int position);
    }

    public interface CancelOrderDeliveryListener {
        void onCancelOrderDeliveryClick(int position);
    }

    public interface SeeLogisticsReceivingListener {
        void onSeeLogisticsReceivingClick(int position);
    }

    public interface ConfirmReceivingListener {
        void onConfirmReceivingClick(int position);
    }

    public interface RefundGoodsListener {
        void onRefundGoodsClick(int position);
    }

    public interface SeeLogisticsEvaluateListener {
        void onSeeLogisticsEvaluateClick(int position);
    }

    public interface EvaluateListener {
        void onEvaluateClick(int position);
    }

    private PayListener mPayListener;
    private CancelOrderPaymentListener mCancelOrderPaymentListener;
    private CancelOrderDeliveryListener mCancelOrderDeliveryListener;
    private SeeLogisticsReceivingListener mSeeLogisticsReceivingListener;
    private ConfirmReceivingListener mConfirmReceivingListener;
    private RefundGoodsListener mRefundGoodsListener;
    private SeeLogisticsEvaluateListener mSeeLogisticsEvaluateListener;
    private EvaluateListener mEvaluateListener;

    public void setPayListener(PayListener listener) {
        this.mPayListener = listener;
    }

    public void setCancelOrderPaymentListener(CancelOrderPaymentListener listener) {
        this.mCancelOrderPaymentListener = listener;
    }

    public void setCancelOrderDeliveryListener(CancelOrderDeliveryListener listener) {
        this.mCancelOrderDeliveryListener = listener;
    }

    public void setSeeLogisticsReceivingListener(SeeLogisticsReceivingListener listener) {
        this.mSeeLogisticsReceivingListener = listener;
    }

    public void setConfirmReceivingListener(ConfirmReceivingListener listener) {
        this.mConfirmReceivingListener = listener;
    }

    public void setRefundGoodsListener(RefundGoodsListener listener) {
        this.mRefundGoodsListener = listener;
    }

    public void setSeeLogisticsEvaluateListener(SeeLogisticsEvaluateListener listener) {
        this.mSeeLogisticsEvaluateListener = listener;
    }

    public void setEvaluateListener(EvaluateListener listener) {
        this.mEvaluateListener = listener;
    }

    public void clearData() {
        mOrderList.clear();
        notifyDataSetChanged();
    }

}
